home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Modules / mathmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-28  |  5.6 KB  |  251 lines  |  [TEXT/CWIE]

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Math module -- standard C math library functions, pi and e */
  33.  
  34. #include "allobjects.h"
  35.  
  36. #include <errno.h>
  37.  
  38. #define getdoublearg(v, a) getargs(v, "d", a)
  39. #define get2doublearg(v, a, b) getargs(v, "(dd)", a, b)
  40.  
  41. #include "mymath.h"
  42.  
  43. #ifndef _MSC_VER
  44. #ifndef __STDC__
  45. extern double fmod PROTO((double, double));
  46. extern double frexp PROTO((double, int *));
  47. extern double ldexp PROTO((double, int));
  48. extern double modf PROTO((double, double *));
  49. #endif /* __STDC__ */
  50. #endif /* _MSC_VER */
  51.  
  52.  
  53. #ifdef i860
  54. /* Cray APP has bogus definition of HUGE_VAL in <math.h> */
  55. #undef HUGE_VAL
  56. #endif
  57.  
  58. #ifdef HUGE_VAL
  59. #define CHECK(x) if (errno != 0) ; \
  60.     else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
  61.     else errno = ERANGE
  62. #else
  63. #define CHECK(x) /* Don't know how to check */
  64. #endif
  65.  
  66. static object *
  67. math_error()
  68. {
  69.     if (errno == EDOM)
  70.         err_setstr(ValueError, "math domain error");
  71.     else if (errno == ERANGE)
  72.         err_setstr(OverflowError, "math range error");
  73.     else
  74.         err_errno(ValueError); /* Unexpected math error */
  75.     return NULL;
  76. }
  77.  
  78. static object *
  79. math_1(args, func)
  80.     object *args;
  81.     double (*func) FPROTO((double));
  82. {
  83.     double x;
  84.     if (!getdoublearg(args, &x))
  85.         return NULL;
  86.     errno = 0;
  87.     x = (*func)(x);
  88.     CHECK(x);
  89.     if (errno != 0)
  90.         return math_error();
  91.     else
  92.         return newfloatobject(x);
  93. }
  94.  
  95. static object *
  96. math_2(args, func)
  97.     object *args;
  98.     double (*func) FPROTO((double, double));
  99. {
  100.     double x, y;
  101.     if (!get2doublearg(args, &x, &y))
  102.         return NULL;
  103.     errno = 0;
  104.     x = (*func)(x, y);
  105.     CHECK(x);
  106.     if (errno != 0)
  107.         return math_error();
  108.     else
  109.         return newfloatobject(x);
  110. }
  111.  
  112. #define FUNC1(stubname, func) \
  113.     static object * stubname(self, args) object *self, *args; { \
  114.         return math_1(args, func); \
  115.     }
  116.  
  117. #define FUNC2(stubname, func) \
  118.     static object * stubname(self, args) object *self, *args; { \
  119.         return math_2(args, func); \
  120.     }
  121.  
  122. FUNC1(math_acos, acos)
  123. FUNC1(math_asin, asin)
  124. FUNC1(math_atan, atan)
  125. FUNC2(math_atan2, atan2)
  126. FUNC1(math_ceil, ceil)
  127. FUNC1(math_cos, cos)
  128. FUNC1(math_cosh, cosh)
  129. FUNC1(math_exp, exp)
  130. #ifdef __MWERKS__
  131. double myfabs(double x) { return fabs(x); }
  132. FUNC1(math_fabs, myfabs)
  133. #else
  134. FUNC1(math_fabs, fabs)
  135. #endif
  136. FUNC1(math_floor, floor)
  137. FUNC2(math_fmod, fmod)
  138. FUNC2(math_hypot, hypot)
  139. FUNC1(math_log, log)
  140. FUNC1(math_log10, log10)
  141. #ifdef MPW_3_1 /* This hack is needed for MPW 3.1 but not for 3.2 ... */
  142. FUNC2(math_pow, power)
  143. #else
  144. FUNC2(math_pow, pow)
  145. #endif
  146. FUNC1(math_sin, sin)
  147. FUNC1(math_sinh, sinh)
  148. FUNC1(math_sqrt, sqrt)
  149. FUNC1(math_tan, tan)
  150. FUNC1(math_tanh, tanh)
  151.  
  152.  
  153. static object *
  154. math_frexp(self, args)
  155.     object *self;
  156.     object *args;
  157. {
  158.     double x;
  159.     int i;
  160.     if (!getdoublearg(args, &x))
  161.         return NULL;
  162.     errno = 0;
  163.     x = frexp(x, &i);
  164.     CHECK(x);
  165.     if (errno != 0)
  166.         return math_error();
  167.     return mkvalue("(di)", x, i);
  168. }
  169.  
  170. static object *
  171. math_ldexp(self, args)
  172.     object *self;
  173.     object *args;
  174. {
  175.     double x, y;
  176.     /* Cheat -- allow float as second argument */
  177.     if (!get2doublearg(args, &x, &y))
  178.         return NULL;
  179.     errno = 0;
  180.     x = ldexp(x, (int)y);
  181.     CHECK(x);
  182.     if (errno != 0)
  183.         return math_error();
  184.     else
  185.         return newfloatobject(x);
  186. }
  187.  
  188. static object *
  189. math_modf(self, args)
  190.     object *self;
  191.     object *args;
  192. {
  193.     double x, y;
  194.     if (!getdoublearg(args, &x))
  195.         return NULL;
  196.     errno = 0;
  197. #ifdef MPW /* MPW C modf expects pointer to extended as second argument */
  198. {
  199.     extended e;
  200.     x = modf(x, &e);
  201.     y = e;
  202. }
  203. #else
  204.     x = modf(x, &y);
  205. #endif
  206.     CHECK(x);
  207.     if (errno != 0)
  208.         return math_error();
  209.     return mkvalue("(dd)", x, y);
  210. }
  211.  
  212. static struct methodlist math_methods[] = {
  213.     {"acos", math_acos},
  214.     {"asin", math_asin},
  215.     {"atan", math_atan},
  216.     {"atan2", math_atan2},
  217.     {"ceil", math_ceil},
  218.     {"cos", math_cos},
  219.     {"cosh", math_cosh},
  220.     {"exp", math_exp},
  221.     {"fabs", math_fabs},
  222.     {"floor", math_floor},
  223.     {"fmod", math_fmod},
  224.     {"frexp", math_frexp},
  225.     {"hypot", math_hypot},
  226.     {"ldexp", math_ldexp},
  227.     {"log", math_log},
  228.     {"log10", math_log10},
  229.     {"modf", math_modf},
  230.     {"pow", math_pow},
  231.     {"sin", math_sin},
  232.     {"sinh", math_sinh},
  233.     {"sqrt", math_sqrt},
  234.     {"tan", math_tan},
  235.     {"tanh", math_tanh},
  236.     {NULL,        NULL}        /* sentinel */
  237. };
  238.  
  239. void
  240. initmath()
  241. {
  242.     object *m, *d, *v;
  243.     
  244.     m = initmodule("math", math_methods);
  245.     d = getmoduledict(m);
  246.     dictinsert(d, "pi", v = newfloatobject(atan(1.0) * 4.0));
  247.     DECREF(v);
  248.     dictinsert(d, "e", v = newfloatobject(exp(1.0)));
  249.     DECREF(v);
  250. }
  251.